<?php

/**
 * pelanggaran actions.
 *
 * @package    alazhar
 * @subpackage pelanggaran
 * @author     Ardhy Widodo
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pelanggaranActions extends sfActions {

    public function __construct($context, $moduleName, $actionName) {
        parent::__construct($context, $moduleName, $actionName);
        $this->getUser()->setAttribute('subsystem', 'bk');
        $this->getUser()->setAttribute('module', 'pelanggaran');
    }

    public function executeIndex(sfWebRequest $request) {
        $this->sejarah_pelanggarans = Doctrine_Core::getTable('SejarahPelanggaran')
                ->createQuery('a')
                ->innerJoin('a.Siswa s')
                ->innerJoin('a.JenisPelanggaran j')
                ->orderBy($request->getParameter('sortby', 'a.id') . ' ' . $request->getParameter('sortmode', 'asc'))
                ->execute();
        if ($request->getParameter('sortmode') == 'desc') {
            $this->sortmode = 'asc';
        } else {
            $this->sortmode = 'desc';
        }
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new SejarahPelanggaranForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new SejarahPelanggaranForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($sejarah_pelanggaran = Doctrine_Core::getTable('SejarahPelanggaran')->find(array($request->getParameter('id'))), sprintf('Object sejarah_pelanggaran does not exist (%s).', $request->getParameter('id')));
        $this->form = new SejarahPelanggaranForm($sejarah_pelanggaran);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($sejarah_pelanggaran = Doctrine_Core::getTable('SejarahPelanggaran')->find(array($request->getParameter('id'))), sprintf('Object sejarah_pelanggaran does not exist (%s).', $request->getParameter('id')));
        $this->form = new SejarahPelanggaranForm($sejarah_pelanggaran);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($sejarah_pelanggaran = Doctrine_Core::getTable('SejarahPelanggaran')->find(array($request->getParameter('id'))), sprintf('Object sejarah_pelanggaran does not exist (%s).', $request->getParameter('id')));
        $sejarah_pelanggaran->delete();

        $this->redirect('pelanggaran/index');
    }

    public function executeJsonsiswa(sfWebRequest $request) {
        if ($siswa = Doctrine_Core::getTable('Siswa')->findOneByNoInduk($request->getParameter('no_induk'))) {
            $usia = date('Y') - date_parse($siswa->tanggal_lahir)['year'];
            $data = array('id' => $siswa->id, 'nama' => $siswa->nama_lengkap, 'jk' => $siswa->jk, 'usia' => $usia);
            $data['success'] = true;
        } else {
            $data['success'] = false;
        }

        $json = json_encode($data, true);
        $this->getResponse()->setHttpHeader('Content-type', 'application/json');
        $this->renderText($json);
        return sfView::NONE;
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $sejarah_pelanggaran = $form->save();

            $this->redirect('pelanggaran/edit?id=' . $sejarah_pelanggaran->getId());
        }
    }

}
